#include "../global.h"

#include <QHBoxLayout>

class vtkTIPWCallback : public vtkCommand
{
public:
    static vtkTIPWCallback *New()
    {
        return new vtkTIPWCallback;
    }

    virtual void Execute(vtkObject *caller, unsigned long, void*)
    {
        vtkPlaneWidget *pWidget = vtkPlaneWidget::SafeDownCast(caller);
        pWidget->GetPlane(m_plane);
        m_vMapper->AddClippingPlane(m_plane);
        m_volume->VisibilityOn();
    }

    void setParameter(vtkPlane *p, vtkVolume *v, vtkFixedPointVolumeRayCastMapper *m)
    {
        m_plane = p;
        m_volume = v;
        m_vMapper = m;
    }
protected:
    vtkTIPWCallback():m_plane(0),m_volume(0),m_vMapper(0){}
    vtkPlane *m_plane;
    vtkVolume *m_volume;
    vtkFixedPointVolumeRayCastMapper *m_vMapper;

};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    vtkCreate(reader, vtkDICOMImageReader);
    reader->SetDataByteOrderToLittleEndian();
    reader->SetDirectoryName(path.toStdString().c_str());
    reader->Update();

    vtkWidgetCreate(mainWidget, QWidget);
    QHBoxLayout *box = new QHBoxLayout(mainWidget);

    vtkCreate(renderer, vtkRenderer);
    vtkWidgetCreate(widget, QVTKWidget);
    QVTKWidget* vtkWidget = widget.data();
    vtkWidget->GetRenderWindow()->AddRenderer(renderer);
    vtkCreate(renderer2, vtkRenderer);
    vtkWidgetCreate(widget2, QVTKWidget);
    QVTKWidget* vtkWidget2 = widget2.data();
    vtkWidget2->GetRenderWindow()->AddRenderer(renderer2);
    box->addWidget(vtkWidget);
    box->addWidget(vtkWidget2);

    vtkCreate(shift, vtkImageCast);
    shift->SetInputConnection(reader->GetOutputPort());
    shift->SetOutputScalarTypeToUnsignedShort();
    shift->Update();

    vtkCreate(opacityTransferFunction, vtkPiecewiseFunction);
    opacityTransferFunction->AddSegment(0, 0, 3000, 1);

    vtkCreate(colorTransferFunction, vtkColorTransferFunction);
    colorTransferFunction->AddRGBSegment(0, 1, 1, 1, 255, 1, 1, 1);

    vtkCreate(mapper, vtkFixedPointVolumeRayCastMapper);
    mapper->SetInputConnection(reader->GetOutputPort());
    mapper->SetBlendModeToMaximumIntensity();

    vtkCreate(property, vtkVolumeProperty);
    property->SetColor(colorTransferFunction);
    property->SetScalarOpacity(opacityTransferFunction);
    property->SetInterpolationTypeToLinear();
    property->ShadeOff();
    property->SetAmbient(0.2);
    property->SetDiffuse(0.9);
    property->SetSpecular(0.2);
    property->SetSpecularPower(10);

    vtkCreate(volume, vtkVolume);
    volume->SetProperty(property);
    volume->SetMapper(mapper);

    vtkCreate(plane, vtkPlane);
    plane->SetOrigin(reader->GetOutput()->GetCenter());
    plane->SetNormal(1, 0, 0);

    vtkCreate(myCallback, vtkTIPWCallback);
    volume->SetMapper(mapper);
    volume->SetProperty(property);
    myCallback->setParameter(plane, volume, mapper);

    vtkCreate(planeWidget, vtkPlaneWidget);
    planeWidget->SetInteractor(vtkWidget->GetInteractor());
    planeWidget->SetInputData(shift->GetOutput());
    planeWidget->SetResolution(10);
    planeWidget->GetPlaneProperty()->SetColor(.9, .4, .4);
    planeWidget->GetHandleProperty()->SetColor(0, .4, .7);
    planeWidget->GetHandleProperty()->SetLineWidth(1.5);
    planeWidget->NormalToYAxisOn();
    planeWidget->SetRepresentationToSurface();
    planeWidget->SetCenter(shift->GetOutput()->GetCenter());
    planeWidget->PlaceWidget();
    planeWidget->AddObserver(vtkCommand::InteractionEvent,myCallback);
    planeWidget->On();

    vtkCreate(cutter, vtkCutter);
    cutter->SetCutFunction(plane);
    cutter->SetInputConnection(shift->GetOutputPort());
    cutter->Update();

    vtkCreate(selectMapper, vtkPolyDataMapper);
    selectMapper->SetInputConnection(cutter->GetOutputPort());

    vtkCreate(actor, vtkActor);
    actor->SetMapper(selectMapper);

    renderer->AddVolume(volume);
    renderer2->AddActor(actor);
    renderer->SetBackground(0, 0, 0);

    mainWidget->resize(WINDOW_WIDTH, WINDOW_HEIGHT);
    mainWidget->show();

    return a.exec();
}
